﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

using ECCIT;
using ECCIT.Extension.Weibo_v2.Model;
using ECCIT.Extension.Weibo_v2.Common;

using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.Configuration;
using OgilvyOne.UKNow.Mobile.Model;
using OgilvyOne.UKNow.Mobile.Utilities;

namespace OgilvyOne.UKNow.Mobile.APIs.News
{
    public class GetList : IHttpHandler
    {
        public bool IsReusable
        {
            get { throw new NotImplementedException(); }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string sPageSize = context.Request.Params["page_size"];
                string sCurrPageIndex = context.Request.Params["current_page"];

                int pageSize = 10;
                if (!string.IsNullOrEmpty(sPageSize))
                    pageSize = int.Parse(sPageSize);

                int currPageIndex = 1;
                if (!string.IsNullOrEmpty(sCurrPageIndex))
                    currPageIndex = int.Parse(sCurrPageIndex);

                NewsBLL bll = new NewsBLL(2052);
                PagingList list = bll.GetPagingNewsModel(currPageIndex-1, pageSize);
                List<NewsInfo> news = new List<NewsInfo>();
                foreach (NewsModel model in list.List)
                {
                    NewsInfo ni = new NewsInfo();
                    //ni.Content = model.Content;
                    ni.Content = model.PhoneContent;

                    if (!string.IsNullOrEmpty(model.HtmlFileName))
                        ni.HtmlFileName = ConfigCacheManager.DomainName + "/cn/news/" + model.HtmlFileName;
                    else
                        ni.HtmlFileName = string.Format("{0}/cn/news/{1}.html", ConfigCacheManager.DomainName, model.ID);

                    ni.Images = ModelConvertUtility.GetNewPhoneImages(model.ID);


                    //if (!string.IsNullOrEmpty(model.ImageName))
                    //    ni.ImageName = ConfigCacheManager.DomainName + ConfigCacheManager.FilePath.GetImageVirtualPath(model.ImageName);

                    ni.ID = model.ID;
                    ni.Title = model.Title;
                    ni.InsertedTime = DateTimeFormatter.FormatDateTime(model.InsertedTime);

                    news.Add(ni);
                }

                NewsListInfo nli = new NewsListInfo();
                nli.List = news;
                nli.CurrentPageIndex = currPageIndex;
                nli.PageCount = list.Capacity % pageSize == 0 ? list.Capacity / pageSize : list.Capacity / pageSize + 1;
                nli.TotalCount = list.Capacity;
                string s = JsonSerializationHelper.ObjectToJson(nli);
                context.Response.Write(ResultUtility.ReturnResultJson("0", s, "获取活动亮点数据列表成功"));
            }
            catch (Exception ex)
            {
                context.Response.Write(ResultUtility.ReturnResultJson("-101", "", "获取活动亮点数据列表失败. Message:" + ex.Message));
            }
        }
    }
}
